#include <pthread.h>
#include "font.h"
#include "data.h"
#include "init_uninit.h"
#include "get_x_y.h"
#include "user.h"
#include"show_bmp.h"
#include"time_local_and_utc.h"

//改变目录的标志
int change_dir_index = 0;

//线程结束和开始标志
int thread_exit_flag = 0;

// 状态变量，用于控制程序流程   主进程
int n = 0; 

// 标志变量，用于控制循环  主线程
int continueDisplay = 1; 

//标志变量 用于切换图片和视频
int change_picture_or_video = 0;

//标志变量 用于切换时区 暂时未用到
int change_time = 0;

//目标文件的标识符
int dir_fd;

//写死的地理位置
char *buff[] = {"中国","广东省","云南省","广西省","浙江省","江西省","湖北省","湖南省",
                "江苏省","四川省","海南省","福建省","青海省","安徽省","河南省","陕西省",
                "山西省","山东省","河南省","河北省","北京市","天津市","上海市","重庆市",
                "黑龙江省","吉林省","辽宁省","内蒙古自治区","新疆自治区","新疆自治区","西藏自治区","宁夏自治区","贵州省"};

//标志变量 用于切换地理位置
int location_data = 0;

//线程
void *pthread_fun1(void *arg);
void *pthread_fun2(void *arg);
void *pthread_fun3(void *arg);

int main(void)
{
    pthread_t thread1,thread2,thread3,thread4;
    while (1) 
    {
        switch (n) // 根据状态变量n来决定执行的流程
        {
            case 0:
                show_one(); 
                while (continueDisplay)
                {
                    if(pthread_create(&thread1, NULL, pthread_fun1, NULL) != 0)
                    {
                        perror("fail to pthread_create");
                    }
                    if(pthread_create(&thread2, NULL, pthread_fun2, NULL) != 0)
                    {
                        perror("fail to pthread_create");
                    }
                    if(pthread_create(&thread3, NULL, pthread_fun3, NULL) != 0)
                    {
                        perror("fail to pthread_create");
                    }
                    show_location(buff,&location_data);//地理位置
                    show_weather();//天气
                    while (1)
                    {
                        get_xy(); // 获取触摸坐标   检测是否触摸了管理员登录区域
                        if (Y < 100 && X > 650) 
                        {
                            thread_exit_flag = 1; // 设置退出标志
                            system("killall -9 mplayer");
                            pthread_join(thread1, NULL); // 清理线程资源
                            pthread_join(thread2, NULL); // 清理线程资源
                            pthread_join(thread3, NULL); // 清理线程资源
                            continueDisplay = 0; // 设置标志变量，退出循环
                            break;
                        }
                    }
                }
                n = 1; // 状态变更为1 跳转到密码页面
                break;
            case 1:
                show_password();
                n = 2; // 状态变更为2  判断密码输入
                break;
            case 2:
                password(&n,&thread_exit_flag,&continueDisplay,&change_picture_or_video);  //执行密码的逻辑
                break;
            case 3:
                show_user();
                user_function(&n,&thread_exit_flag,&continueDisplay,&change_picture_or_video,&change_dir_index);//管理员的功能
                break;
        }
    }
}

/***************************** 
 * 函数功能：线程执行图片视频循环
*******************************/
void *pthread_fun1(void *arg)
{
    if (change_picture_or_video == 0)
    {
        const char *filename;
        if (change_dir_index == 0)
        {
            filename = "pictures.txt";
        }
        else
        {
            filename = "/mnt/udisk/test/pictures.txt";
        }
        
        // 初始化头节点为NULL
        Node head = NULL; 
        printf("从文件加载图片名称...\n");
        load_pictures_from_file(&head, filename);

        while (!thread_exit_flag)
        {
                show_bmp_xy(head->next->filename,15,150);
                sleep(3);
                head = head->next;
        }
    }
    else if(change_picture_or_video == 1)
    {
        const char *filename;
        if (change_dir_index == 0)
        {
            filename = "mp4.txt";
        }
        else
        {
            filename = "/mnt/udisk/test/mp4.txt";
        }
        
        Node head = NULL; // 初始化头节点为NULL
        // 从文件加载图片名称
        printf("从文件加载视频名称...\n");
        load_pictures_from_file(&head, filename);
        while (!thread_exit_flag)
        {
            char *mp4filename = head->filename;
            show_mplayer(&change_dir_index,mp4filename);
            //1ms  线程当中至少保证有1ms延时，保证线程会交出CPU资源
            head = head->next;
            sleep(2);
        }  
    } 
    else
    {
        const char *filename;
        if (change_dir_index == 0)
        {
            filename = "pictures.txt";
        }
        else
        {
            filename = "/mnt/udisk/test/pictures.txt";
        }
        Node head = NULL; // 初始化头节点为NULL
        printf("从文件加载图片名称...\n");
        load_pictures_from_file(&head, filename);
        Node current = head;

        const char *filename_mp4;
        if (change_dir_index == 0)
        {
            filename_mp4 = "mp4.txt";
        }
        else
        {
            filename_mp4 = "/mnt/udisk/test/mp4.txt";
        }
        
        Node head_mp4 = NULL; // 初始化头节点为NULL
        // 从文件加载图片名称
        printf("从文件加载视频名称...\n");
        load_pictures_from_file(&head_mp4, filename_mp4);
        Node current_mp4 = head_mp4;

        while (!thread_exit_flag)
        {
            show_bmp_xy(current->filename,15,150);
            sleep(2);
            show_bmp_xy(current->next->filename,15,150);
            sleep(2);
            current = current->next->next;
            show_mplayer(&change_dir_index,current_mp4->filename);
            //2s  线程当中至少保证有1ms延时，保证线程会交出CPU资源
            current_mp4 = current_mp4->next;
            sleep(2);
        }
    }
}

/***************************** 
 * 线程执行广告词语循环
******************************/
void *pthread_fun2(void *arg)
{  
    const char *filename;
    if (change_dir_index == 0)
    {
        filename = "gg.txt";
    }
    else
    {
        filename = "/mnt/udisk/test/gg.txt";
    }
    // 初始化头节点为NULL
    Node head = NULL; 
    printf("从文件加载图片名称...\n");
    load_pictures_from_file(&head, filename);
    
    //将字体写到点阵图上
    int i = 0;
    while (!thread_exit_flag)
    {
        //初始化Lcd
        struct LcdDevice* lcd = init_lcd("/dev/fb0");
                
        //打开字体	
        font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
        
        //字体大小的设置
        fontSetSize(f,50);
            
        //创建一个画板（点阵图）
        bitmap *bm = createBitmapWithInit(570,50,4,getColor(0,255,255,255)); 

        fontPrint(f,bm,0,0,head->filename,getColor(0,255,0,0),0);
        head = head->next;
        
        //把字体框输出到LCD屏幕上
        show_font_to_lcd(lcd->mp,23,35,bm);
        
        //关闭字体，关闭画板
        fontUnload(f);
        destroyBitmap(bm);
        sleep(2); // 等待2秒
    }
    
}

/***************************** 
 * 线程执行时间
 *****************************/
void *pthread_fun3(void *arg)
{
    while (!thread_exit_flag)
    {
        char *current_time_str;
        if(change_time == 0)
        {
            current_time_str = getLocalNowtime();
        }
        else
        {
            current_time_str = getUTCNowtime();
        }

        //初始化Lcd
        struct LcdDevice* lcd = init_lcd("/dev/fb0");
                
        //打开字体	
        font *f = fontLoad("/usr/share/fonts/DroidSansFallback.ttf");
        
        //字体大小的设置
        fontSetSize(f,20);
            
        //创建一个画板（点阵图）
        bitmap *bm = createBitmapWithInit(138,30,4,getColor(0,255,255,255)); 

        fontPrint(f,bm,0,0,current_time_str,getColor(0,255,0,0),0);
        
        //把字体框输出到LCD屏幕上
        show_font_to_lcd(lcd->mp,648,204,bm);
        
        //关闭字体，关闭画板
        fontUnload(f);
        destroyBitmap(bm);
        // 释放由getNowtime分配的内存
        free(current_time_str);
        sleep(1); // 休眠1秒
    }   
}
